package com.playcrab.adouban;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast; 

import com.google.gdata.client.douban.DoubanService;
import com.google.gdata.data.Person;
import com.google.gdata.data.PlainTextConstruct;
import com.google.gdata.data.douban.CollectionEntry;
import com.google.gdata.data.douban.DoumailEntry;
import com.google.gdata.data.douban.MiniblogEntry;
import com.google.gdata.util.ParseException;
import com.google.gdata.util.ServiceException;

public class ComposeDoumail extends BaseActivity {
	private static final String LOG_TAG = "ComposeDoumail";
	private Handler handler = new Handler();
	private String selectedStatus = null;
	
	 
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
		
		setContentView(R.layout.compose_doumail);
		checkLoggedin();
		
		
		Intent intent = getIntent();
		Bundle bundle = intent.getExtras(); 
		final String to  = bundle.getString("to"); 
		final String title  = bundle.getString("title");
		final String body  = bundle.getString("body");
	    String toUserTitle = bundle.getString("toUserTitle");
		
		 
		setTitle(getString(R.string.compose_doumail));
		
		if(toUserTitle==null)
			toUserTitle  =  to.substring(to.lastIndexOf('/')+1);
		
		TextView toEditText = (TextView) findViewById(R.id.compose_doumail_to);
		toEditText.setText(toUserTitle);
		
		 
		if(title!=null){
			EditText titleEditText = (EditText) findViewById(R.id.compose_doumail_title_edit);
			titleEditText.setText(title);
		}
		if(body!=null){
			EditText bodyEditText = (EditText) findViewById(R.id.compose_doumail_body_edit);
			bodyEditText.setText(body);
		}
		
        final Button button = (Button) findViewById(R.id.compose_doumail_button);
		OnClickListener listener = new OnClickListener() {
			public void onClick(View v) { 
				final String title = ((EditText) findViewById(R.id.compose_doumail_title_edit)) .getText().toString();
				final String body = ( (EditText)findViewById(R.id.compose_doumail_body_edit) ).getText().toString();
			  
 				button.refreshDrawableState(); 
				final DoubanService myService = getAuthedDoubanService();
				if(myService == null){
					return;
				} 
 				final Toast t = Toast.makeText(getApplicationContext(), 
						ComposeDoumail.this.getResources().getText(R.string.compose_doumail_failed),
						Toast.LENGTH_SHORT);
					new Thread() {
						public void run() { 
							handler.post(new Runnable() {
								public void run() {
									DoumailEntry entry;
									try {
										Person toUser = new Person();
										toUser.setUri(to);
										entry = myService.createDoumail(toUser, new PlainTextConstruct(title), new PlainTextConstruct(body) ) ;
										 
										if(entry != null){
											t.setText(ComposeDoumail.this.getResources().getText(R.string.compose_doumail_success)); 
											Intent i = new Intent(ComposeDoumail.this, Doumail.class);
											i.putExtra("type", "outbox");
											startActivity(i);
											ComposeDoumail.this.finish();
										}
									}catch(ParseException e){ 
										if(e.getResponseBody() == null){
											e.printStackTrace();
											t.setText(ComposeDoumail.this.getResources().getText(R.string.compose_doumail_success)); 
											Intent i = new Intent(ComposeDoumail.this, Doumail.class);
											i.putExtra("type", "outbox");
											startActivity(i);
											ComposeDoumail.this.finish();
										}else{
											t.setText(ComposeDoumail.this.getResources().getText(R.string.compose_doumail_over)); 
										} 
									} catch (IOException e) {   
										ComposeDoumail.this.showInfo(getString(R.string.network_error));
									} catch (ServiceException e) {  
										ComposeDoumail.this.showInfo(getString(R.string.service_error));
									} finally {
										t.show();
									}
								}
							} );
						}
					}.start();
					
				
			}
		};
		
		
		button.setOnClickListener(listener);
		
		 Button cancelButton = (Button) findViewById(R.id.compose_doumail_cancel_button);
		
		OnClickListener cancelListener = new OnClickListener() {
			public void onClick(View v) { 
				ComposeDoumail.this.finish();
			}
		};
		cancelButton.setOnClickListener(cancelListener);

	}

	public void onStart() {
		super.onStart();
	}

	public void onStop() {
		super.onStop();
	}
}
